{extend name="mainadmin@layouts/base" /}
{block name="main" }

<header class="header  b-b clearfix">
     <div class="page-breadcrumbs">
            <ul class="breadcrumb">
                <li>
                    <i class="fa fa-ellipsis-v"></i>
                    <strong>{$row['award_id']>0?'编辑':'添加'}奖项</strong>
                </li>                                  
            </ul>
           <a class="text-muted pull-right pointer p-r m-t-md" data-toggle="back" title="返回"><i class="fa fa-reply"></i></a>
        </div>
</header>
<form class="form-horizontal form-validate form_vbox" method="post" action="{:url('info')}">
    <section class="vbox">
        <section class="scrollable  wrapper w-f">
            <section class="panel panel-default">
                <div class="panel-body">
                    <div class="form-group">
                        <label class="control-label">奖项名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="input-large" data-rule-maxlength="20" data-rule-required="true"
                                   name="award_name" value="{$row.award_name}"><span class="maroon">*</span>
                        </div>
                    </div>
                    <div class="form-group ">
                        <label class="control-label">参与分销身份：</label>
                        <div class="col-sm-9">
                            <li style="list-style:none; float:left; margin:2px;">
                                <label><input type="checkbox" name="limit_role[]" data-role_name="粉丝" class="selectRole"
                                              value="0" {:in_array(0,$row['limit_role'])?'checked':''}> 粉丝</label>
                            </li>
                            {volist name="UsersRole" id="urow" }
                            <li style="list-style:none; float:left; margin:2px;">
                                <label><input type="checkbox" name="limit_role[]" data-role_name="{$urow.role_name}"
                                              class="selectRole"
                                              value="{$urow.role_id}" <?=in_array($urow['role_id'],$row['limit_role'])?'checked':'';?>
                                    > {$urow.role_name}</label>
                            </li>
                            {/volist}
                        </div>
                    </div>
                    <div class="form-group">
                        <label class=" control-label">购买商品：</label>
                        <div class="col-sm-9">
                            <label><input type="radio" name="goods_limit" value="1" class="js_radio_undertake" {$row['goods_limit']<=1?'checked':''}
                                > 购买任意分销商品</label>
                            <label><input type="radio" name="goods_limit" value="2" class="js_radio_undertake"
                                          data-class="buy_goods" {$row['goods_limit']==2?'checked':''}>
                                购买指定分销商品</label>

                            <div class="radio_undertake_goods_limit buy_goods  {$row['goods_limit']==2?'':'hide'}"></div>


                        </div>
                    </div>

                    <div class="form-group">
                        <label class=" control-label">奖项类型：</label>
                        <div class="col-sm-9">
                            <label><input type="radio" name="award_type" value="1" class="js_radio_undertake"
                                          data-class="award_setting|ordinary_type" {$row['award_type']<=1?'checked':''}>
                                普通分销</label>
                            <label><input type="radio" name="award_type" value="2" class="js_radio_undertake "
                                          data-class="repeat_limit_val|assigned_manage_tip|award_manage_setting" {$row['award_type']==2?'checked':''}
                                onclick="evelLimitRole()"> 业绩返点奖</label>
                            <label><input type="radio" name="award_type" value="3" class="js_radio_undertake"
                                          data-class="role_award_setting" {$row['award_type']==3?'checked':''}>
                                身份分销</label>
                            <div>
                                <label class="radio_undertake_award_type assigned_manage_tip  {$row['award_type']==2?'':'hide'}">
                                    一、根据上级是【粉丝】则拿【粉丝】的奖励，再续续往找上级<br>
                                    二、直属上级身份跟购买会员身份一致，如果设置平级奖则拿平级（不扣减上级奖励），不设置则拿对应身份奖励，直接跳过设置-1<br>
                                    三、最高奖励数量为最高级别的奖励设置的数量，每级递减.<br>
                                    四、算法：低级身份10元，次级身份20元，高级身份30元<br>
                                    1、低级身份分佣10元，则次级身份佣金也是10元<br>
                                    2、直属上级为次级身份则直接拿20元<br>
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group radio_undertake_award_type ordinary_type {$row['award_type']==1?'':'hide'}">
                        <label class=" control-label">普通分销模式：</label>
                        <div class="col-sm-9">
                            <label><input type="radio" name="ordinary_type" value="0" {$row['ordinary_type']==0?'checked':''}>
                                逐级计算（一级奖项对应第一级上级会员，不满足条件奖项不分配，逐级判断）</label>
                            <br>
                            <label><input type="radio" name="ordinary_type" value="1" {$row['ordinary_type']==1?'checked':''}>
                                无限级计算（不满足条件，跳过，一直向上找直到奖项分完或没有上级终止）</label>

                        </div>
                    </div>


                    <div class="line line-dashed line-lg pull-in"></div>
                    <div class="form-group radio_undertake_award_type award_setting {$row['award_type']==1?'':'hide'}">
                        <label class="control-label">奖项设置：</label>
                        <div class="col-sm-8">
                            <table class="table table-hover  table-bordered">
                                <thead>
                                <tr>
                                    <th class="txt_center">级别</th>
                                    <th class="txt_center" width="180">奖励名称</th>
                                    <th class="txt_center">上级奖励</th>
                                    <th class="txt_center" width="70">操作</th>
                                </tr>
                                </thead>
                                <tbody id="d_level_box">


                                </tbody>
                                <tr>
                                    <td colspan="5" align="center">
                                        <button type="button" class="btn btn-default " onClick="addLevel();">增加级别奖励
                                        </button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="form-group radio_undertake_award_type role_award_setting {$row['award_type']== 3?'':'hide'}">
                        <label class="control-label">奖项设置：</label>
                        <div class="col-sm-8">
                            <table class="table table-hover  table-bordered ">
                                <thead>
                                <tr>
                                    <th class="txt_center">级别</th>
                                    <th class="txt_center" width="180">奖励名称</th>
                                    <th class="txt_center">奖励</th>
                                    <th class="txt_center" width="70">操作</th>
                                </tr>
                                </thead>
                                <tbody id="d_role_goods_box">


                                </tbody>
                                <tr>
                                    <td colspan="5" align="center">
                                        <button type="button" class="btn btn-default " onClick="addLevel();">增加级别奖励
                                        </button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div class="form-group radio_undertake_award_type award_manage_setting {$row['award_type']==2?'':'hide'}">
                        <label class="control-label">管理奖项：</label>
                        <div class="col-sm-7">
                            <table class="table table-hover  table-bordered">
                                <thead>
                                <tr>
                                    <th class="txt_center">分销身份</th>
                                    <th class="txt_center">奖励名称</th>
                                    <th class="txt_center">上级奖励</th>
                                    <th class="txt_center">平级奖励</th>
                                </tr>
                                </thead>
                                <tbody id="d_role_box">


                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </section>
        </section>
        <footer class="footer bg-white b-t p-t">
            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-1">
                    <input name="award_id" type="hidden" value="{$row.award_id|intval}">
                    <button type="submit" class="btn btn-primary js_save_submit" data-loading-text="保存中...">保存</button>
                    <button type="button" class="btn btn-default" data-toggle="back">取消</button>
                </div>
            </div>
        </footer>
    </section>
</form>
{assign name="is_dividend" value="1"}
{include file="shop@/sys_admin/goods/sel_goods" /}
{literal}
<script type="text/html" id="d_level_tr">
<tr class="d_level_tr">
	<td align="center">{{val}}<input name="award_value[{{key}}][level]" type="hidden" value="{{key}}" /></td>
	<td ><input type="text" name="award_value[{{key}}][name]" class="input-max" data-rule-required="true" value="{{name}}"></td>
	<td style="text-align: right;">
            <input type="text" name="award_value[{{key}}][num]" class="input-ssmall" min="0.01" data-rule-ismoney="true"  value="{{num}}">
            <select name="award_value[{{key}}][num_type]"><option value="money" {{num_type=='money'?'selected':''}}>固定金额</option><option value="per" {{num_type=='per'?'selected':''}}>%百分比</option></select>
    </td>
    <td align="center"><a href="javascript:;" title="删除" onClick="delLevel({{key}});"><i class="fa fa-trash-o text-muted"></i></a></td>
</tr>
</script>
<script type="text/html" id="d_role_goods_tr">
    <tr class="d_role_goods_tr">
        <td align="center">{{val}}<input name="award_value[{{key}}][level]" type="hidden" value="{{key}}" /></td>
        <td ><input type="text" name="award_value[{{key}}][name]" class="input-max" data-rule-required="true" value="{{name}}"></td>
        <td style="text-align: right;">
            {{each roleList as role index}}
            <p>
                {{role.role_name}}：
                <input type="text" name="award_value[{{key}}][num][{{role.role_id}}]" class="input-ssmall" min="0" data-rule-ismoney="true"  value="{{num[role.role_id]}}">
                <select name="award_value[{{key}}][num_type][{{role.role_id}}]"><option value="money" {{num_type[role.role_id]=='money'?'selected':''}}>固定金额</option><option value="per" {{num_type[role.role_id]=='per'?'selected':''}}>%百分比</option></select>
            </p>
            {{/each}}
        </td>
        <td align="center"><a href="javascript:;" title="删除" onClick="delLevel({{key}});"><i class="fa fa-trash-o text-muted"></i></a></td>
    </tr>
</script>
<script type="text/html" id="d_role_tr">
<tr >
	<td align="center">{{role_name}}<input name="role_award_value[{{role_id}}][role_id]" type="hidden" value="{{role_id}}" /></td>
	<td ><input type="text" name="role_award_value[{{role_id}}][name]" class="input-max" data-rule-required="true" value="{{name}}"></td>
	<td ><input type="text" name="role_award_value[{{role_id}}][num]" class="input-ssmall" min="0.01" data-rule-ismoney="true"   value="{{num}}"><select name="role_award_value[{{role_id}}][num_type]"><option value="money" {{num_type=='money'?'selected':''}}>固定金额</option><option value="per" {{num_type=='per'?'selected':''}}>%百分比</option></select></td>
    <td ><input type="text" name="role_award_value[{{role_id}}][same_num]" class="input-ssmall" min="-1"  value="{{same_num}}"><select name="role_award_value[{{role_id}}][same_num_type]"><option value="money" {{same_num_type=='money'?'selected':''}}>固定金额</option><option value="per" {{num_type=='per'?'selected':''}}>%百分比</option></select></td>

</tr>
</script>
{/literal}
{/block}

{block name="footer"}
<script type="text/javascript">
var d_level = {$d_level|json_encode|raw};

var award_value = {$row['award_value']|raw};
var award_type = {$row['award_type']|intval};
var data = [];
data.select_type = 'buy_goods';
data.goodsList = {$row['buy_goods_list']|json_encode|raw};
$(".buy_goods").html(template("selGoods_tpl",data));
data.goodsList = {$row['repeat_goods_list']|json_encode|raw};
data.select_type = 'repeat_goods';
$(".repeat_goods").html(template("selGoods_tpl",data));


//处理管理奖
function evelLimitRole(){	
	$('#d_role_box').html('');
	$('.selectRole').each(function(i,v){
		if ($(this).prop("checked")) {
			var val = [];
			val.role_name = $(this).data('role_name');
			val.role_id = $(this).val();
			$('#d_role_box').append(template('d_role_tr',val));
		}
	})	
}
//优先加载
if (award_type == 2) {
    var d_userRole = {$UsersRole|json_encode|raw};
    $.each(award_value,function(i,v){
        v.role_name = d_userRole[i].role_name;
        v.role_id = i;
        $('#d_role_box').append(template('d_role_tr',v));
    })
}else if (award_type == 3){
        $.each(award_value,function(i,v){
            v.val = d_level[i];
            v.key = i;
            var roleList = [];
            $('.selectRole').each(function(i,v){
                var val = [];
                val.role_name = $(this).data('role_name');
                val.role_id = $(this).val();
                roleList.push(val);
            })
            v.roleList = roleList;
            $('#d_role_goods_box').append(template('d_role_goods_tr',v));
        })
}else{
    $.each(award_value,function(i,v){
        v.val = d_level[i];
        v.key = i;

        $('#d_level_box').append(template('d_level_tr',v));
    })
}


$('.selectRole').click(function(){
    evelLimitRole();
})


function addLevel(){
    var _award_type = $("input[name='award_type']:checked").val();
    if (_award_type == 3){
        var length = $('.d_role_goods_tr').length;
        if (length >= 3) return _alert("级别不能超过三级！");
        var _level = new Object;
        _level.key = length+1;
        _level.val = d_level[length+1];
        _level.num = [];
        _level.num_type = [];
        var roleList = [];
        $('.selectRole').each(function(i,v){
            var val = [];
            val.role_name = $(this).data('role_name');
            val.role_id = $(this).val();
            roleList.push(val);
        })
        _level.roleList = roleList;
        $('#d_role_goods_box').append(template('d_role_goods_tr',_level));
    }else{
        var length = $('.d_level_tr').length;
        if (length >= 3) return _alert("级别不能超过三级！");
        var _level = new Object;
        _level.key = length+1;
        _level.val = d_level[length+1];
        $('#d_level_box').append(template('d_level_tr',_level));
    }
}
function delLevel(key){
    var _award_type = $("input[name='award_type']:checked").val();
    if (_award_type == 3) {
        var length = $('.d_role_goods_tr').length;
        if (key < length) return _alert("不允许跨级删除！");
        $('.d_role_goods_tr').eq(key - 1).remove();
    }else{
        var length = $('.d_level_tr').length;
        if (key < length) return _alert("不允许跨级删除！");
        $('.d_level_tr').eq(key - 1).remove();
    }

}
</script>

{/block}